import { createRouter, createWebHistory } from "vue-router";
import store from "@/store";

import routerIndex from "./routerIndex";
import routerUser from "./routerUser";
import routerCompany from "./routerCompany";
import routerTest from "./routerTest";

const router = createRouter({
    history: createWebHistory(),
    routes: [...routerIndex, ...routerUser, ...routerCompany, ...routerTest],
});

router.beforeEach(async (to, from, next) => {
    document.title = (to.meta.title ? to.meta.title + " - " : "") + "就业直通车";
    const requiresAuth = to.matched.some((record) => record.meta.requiresAuth);
    // console.log("gdepUserInfo", Object.keys(store.state.gdepUserInfo).length);
    if (requiresAuth && Object.keys(store.state.gdepUserInfo).length == 0) {
        // 检查当前路由，决定重定向到哪个登录页面
        const isCompanyRoute = to.path.startsWith("/company");
        next(isCompanyRoute ? "/companyLogin" : "/login");
    } else {
        next();
    }
});

router.afterEach((to) => {
    window.scrollTo(0, 0);
});

export default router;
